//
//  BirthdayView.m
//  HonlivApp
//
//  Created by admin on 2017/6/26.
//  Copyright © 2017年 honliv. All rights reserved.
//

#import "BirthdayView.h"
#import "BirthdayModel.h"
#import "BirthdayViewModel.h"

@interface BirthdayView ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) UIView *headView;
@property (nonatomic,strong) BirthdayViewModel *viewModel;

@end

@implementation BirthdayView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        _headView = [self createHeaderView];
        [self addSubview:_headView];
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(50, 108, kScreenW - 100, kScreenH * 0.5) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [[UIView alloc] init];
        [self addSubview:_tableView];
        
        [self getDatasArray];

        _tableView.backgroundColor = [Main_BGCOLOR colorWithAlphaComponent:0.8];
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    }
    return self;
}

-(void)getDatasArray{
    _viewModel = [[BirthdayViewModel alloc] init];
    [_viewModel getBirthdayDatasSuccess:^(NSArray *array) {
        _datasArray = [NSArray arrayWithArray:array];
        if (_datasArray.count == 0) {
            [SVProgressHUD showImage:[UIImage imageNamed:@"birthday.png"] status:@"今天没有人生日哦"];
            [self removeFromSuperview];
        }
        [_tableView reloadData];
    }];
    
}
//生日提醒弹出页面的头视图
-(UIView *)createHeaderView{
    CGFloat width = kScreenW - 100;
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(50, 60, width, 44)];
    view.backgroundColor = BAR_BGCOLOR;
    
    UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom];
    back.frame = CGRectMake(width-40, 10, 24,24);
    [back addTarget:self action:@selector(hideBirdhdayView) forControlEvents:UIControlEventTouchUpInside];
    [back setImage:[UIImage imageNamed:@"XXX.png"] forState:UIControlStateNormal];
    [view addSubview:back];
    
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake((width-100)*0.5, 0, 100, 44)];
    title.text = @"今日寿星";
    title.textColor = [UIColor whiteColor];
    title.font = kFont(17);
    [view addSubview:title];
    
    return view;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _datasArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cellId"];
    BirthdayModel *model = _datasArray[indexPath.row];
    NSString *title = [NSString stringWithFormat:@"%@  (%@)",model.name,model.sex];
    cell.textLabel.text = title;
    cell.detailTextLabel.text = model.deptName;

    cell.backgroundColor = [Main_BGCOLOR colorWithAlphaComponent:0.3];
    return cell;
}


-(void)hideView:(HideView)block{
    self.hide = block;
}
-(void)hideBirdhdayView{
    if (self.hide) {
        self.hide();
    }
}


@end
